﻿#if DIRECTSHOW
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DirectShowLib;

namespace WhatsPlayingClient.Audio
{
	public static class FilterUtils
	{
		public static string Stringify(this IBaseFilter filter)
		{
			FilterInfo filterInfo;
			int hr = filter.QueryFilterInfo(out filterInfo);
			DsError.ThrowExceptionForHR(hr);
			FilterState state;
			hr = filter.GetState(1000, out state);
			DsError.ThrowExceptionForHR(hr);
			string vendorInfo;
			hr = filter.QueryVendorInfo(out vendorInfo);
			return string.Format("{1} filter <{0}>", filterInfo.achName, state, vendorInfo);
		}

		public static string Stringify(this IPin pin, bool printConnection = true)
		{
			PinInfo pinInfo;
			int hr = pin.QueryPinInfo(out pinInfo);
			DsError.ThrowExceptionForHR(hr);
			IPin connection;
			hr = pin.ConnectedTo(out connection);
			string connectionInfo;
			if(connection == null)
				connectionInfo = "disconnected";
			else if(printConnection)
				connectionInfo = "connected to " + connection.Stringify(false);
			else
				connectionInfo = "connected";
			return string.Format("{1} pin <{0}> of {2}, {3}", 
				pinInfo.name, pinInfo.dir, pinInfo.filter.Stringify(), connectionInfo);
		}

	}
}
#endif